# @Time : 2024-09-29 21:05
# @Author : Fioman 
# @Phone : 13149920693
"""
设备状态类,应该是单例模式,用来表示设备的各种状态
"""
import threading

from app.send_to_ui_handler import send_order_state_to_ui
from common.produce_logger import ProduceLogger
from db_tools.db_handler import DbHandler

db = DbHandler()
logger = ProduceLogger()


class ZmcStates(object):
    """
    Zmc通用状态类
    """

    def __init__(self):
        self.isConnected = False
        self.online = False
        self.rs = False
        self.run = False


class OfflineZmcStates(ZmcStates):
    """
    离线上料的状态类
    """

    def __init__(self):
        super().__init__()
        self.is_throw_board = True  # 默认是丢板


class DeviceStates(object):
    """
    设备状态类
    """
    __instance = None
    __init_flag = False
    __lock = threading.Lock()

    def __new__(cls, *args, **kwargs):
        if cls.__instance is None:
            with cls.__lock:
                cls.__instance = super(DeviceStates, cls).__new__(cls, *args, **kwargs)
        return cls.__instance

    def __init__(self):

        if not self.__init_flag:
            print("DeviceStates.__init__() 进行初始化")
            self.__init_flag = True
            self.cam_zmc_states = ZmcStates()
            self.cbj_zmc_states = ZmcStates()
            self.cutter_zmc_states = ZmcStates()
            self.main_alarm_info = ""  # 主控制器报警信息
            self.main_alarm_is_visible = False
            self.camera_states = [False, False, False, False, False]
            self.stop_work_flag = True
            self.web_data_is_init = False  # UI数据是否初始化
            self.is_login = False  # 用户是否登录
            self.order_used_key = None  # 当前作业的工单,一开始默认是None
            # 离线上料的状态监控
            self.offline_states = OfflineZmcStates()
            self.offline_alarm_info = ""  # 离线上料报警信息
            self.offline_alarm_is_visible = False
            self.offline_shield = [False, False, False, False, False]
            self.offline_is_auto = True

    def pack_state_to_dict(self):
        res = {
            "offlineZmcStates": {
                "online": self.offline_states.online,
                "rs": self.offline_states.rs,
                "run": self.offline_states.run,
                "throwMode":"throw" if  self.offline_states.is_throw_board else "put"
            },
            "camZmcStates": {
                "online": self.cam_zmc_states.online,
                "rs": self.cam_zmc_states.rs,
                "run": self.cam_zmc_states.run
            },
            "cbjZmcStates": {
                "online": self.cbj_zmc_states.online,
                "rs": self.cbj_zmc_states.rs,
                "run": self.cbj_zmc_states.run,
            },
            "cutterZmcStates": {
                "online": self.cutter_zmc_states.online,
                "rs": self.cutter_zmc_states.rs,
                "run": self.cutter_zmc_states.run,
            },
            "mainAlarmInfo": self.main_alarm_info,
            "mainAlarmInfoVisible": self.main_alarm_is_visible,
            "offlineAlarmInfo": self.offline_alarm_info,
            "offlineAlarmInfoVisible": self.offline_alarm_is_visible,
            "cameraGroupStates": self.camera_states,
            "autoState": not self.stop_work_flag,
            "offlineMode": "auto" if self.offline_is_auto else "manual",
            "shieldStart": self.offline_shield,
            "offlineRadioDefault": "start" if self.offline_states.run else "stop",
            "produceRadioDefault": "start" if self.cam_zmc_states.run or self.cbj_zmc_states.run or
                                              self.cutter_zmc_states.run else "stop",
        }
        return res

    def web_show_data_init(self):
        """
        界面上的信息初始化,主要是工单数据,还有一些其他的数据
        :return:
        """
        # 1. 先获取当前作业的工单的key值,如果没有,或者这个key对应的工单不存在,就在现有的工单中找一个
        result = db.get_order_used_key()
        if not result.state:
            logger.debug(result.info)
        result = db.get_working_order_and_total_orders(result.data)
        if not result.state:
            logger.debug(result.info)
        else:
            send_order_state_to_ui(result.data)
